/**
 * 
 */
package com.java.test.user.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.java.test.controller.BaseController;
import com.java.test.system.dto.LoginAccountInfo;
import com.java.test.user.dto.BlackIpInfo;
import com.java.test.user.dto.IpcheckInfo;
import com.java.test.user.service.MemberService;
import com.java.test.util.HttpErrorCode;
import com.java.test.util.JsonResult;
import com.java.test.util.PageInfo;

/**
 * Administrator
 *
 */
@RestController
@RequestMapping("/ipList")
public class IpListController extends BaseController {

	@Autowired
	private MemberService memberservice;

	@RequestMapping("/ipList")
	public JsonResult ipList(@RequestBody PageInfo<BlackIpInfo> page) {
		BlackIpInfo info = page.getInfo();
		if (info == null)
			info = new BlackIpInfo();
		JsonResult js = new JsonResult();
		PageInfo<BlackIpInfo> resPage = memberservice.selectMemberBlackipList(info, page);
		js.setData(resPage);
		return js;
	}

	@RequestMapping("/deleteIpList")
	public JsonResult deleteIpList(@RequestBody List<BlackIpInfo> info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int[] ids = info.stream().mapToInt(BlackIpInfo::getId).toArray();
			int i = memberservice.deleteBlackIp(ids);
			if (i < 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}

			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(), "会员管理-黑名单-删除ip段(多)" + "");
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;

	}

	@RequestMapping("/updateBlackIpStatus")
	public JsonResult updateBlackIpStatus(@RequestBody BlackIpInfo info, HttpServletRequest request) {
		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = memberservice.updateBlackStatus(info);
			if (i < 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}

			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"会员管理-黑名单-" + (info.getUseStatus() == 1 ? "允许访问" : "禁止访问"));
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

	@RequestMapping("/insertBlackIp")
	public JsonResult insertBlackIp(@RequestBody BlackIpInfo info, HttpServletRequest request) {

		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {

			String[] ips = info.getIpAddress().split("~");
			// 长度大于1需要验证ip段
			if (ips.length > 1) {
				String ip1 = ips[0];
				String ip2 = ips[1];

				
				String ip1s[] = ip1.split("\\.");
				String ip2s[] = ip2.split("\\.");

				for (int i = 0; i < ip1s.length; i++) {
					if (i <= 2) {
						if (!ip1s[i].equals(ip2s[i])) {
							js.setCode(HttpErrorCode.IPISERROR.getCode());
							js.setMessage(HttpErrorCode.IPISERROR.getContent());
							return js;
						}
					}
					if (i > 2) {
						if (Integer.valueOf(ip1s[i]) >= Integer.valueOf(ip2s[i])) {
							js.setCode(HttpErrorCode.IPISERROR.getCode());
							js.setMessage(HttpErrorCode.IPISERROR.getContent());
							return js;
						}
					}
				}
			}

			int i = memberservice.insertBlackIp(info);
			if (i < 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}

			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"会员管理-黑名单-添加ip段" + info.getIpAddress());
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;

	}

	// iP 审查
	@RequestMapping("/ipcheckList")
	public JsonResult ipcheckList(@RequestBody PageInfo<IpcheckInfo> page) {
		IpcheckInfo info = page.getInfo();
		if (info == null)
			info = new IpcheckInfo();
		JsonResult js = new JsonResult();
		PageInfo<IpcheckInfo> resPage = memberservice.selectIpCheckByTimeList(info, page);
		js.setData(resPage);
		return js;
	}

	@RequestMapping("/ipcheckupdateStatus")
	public JsonResult ipcheckupdateStatus(@RequestBody IpcheckInfo info, HttpServletRequest request) {

		JsonResult js = new JsonResult(HttpErrorCode.REQUESTSUCCESS.getCode(),
				HttpErrorCode.REQUESTSUCCESS.getContent());
		try {
			int i = memberservice.updateBlackStatus(info);
			if (i < 0) {
				js.setCode(HttpErrorCode.UPDATEDATAFAIL.getCode());
				js.setMessage(HttpErrorCode.UPDATEDATAFAIL.getContent());
				return js;
			}

			// LOG 记录
			LoginAccountInfo user = (LoginAccountInfo) request.getSession().getAttribute("user");

			systemManageService.insertlogInfo(user.getLoginAccout(), user.getIp(),
					"会员管理-ip审查-" + (info.getAccountStatus() == 1 ? "启用" : "禁用"));
		} catch (Exception e) {
			e.printStackTrace();
			js.setCode(HttpErrorCode.SERVICEERROR.getCode());
			js.setMessage(HttpErrorCode.SERVICEERROR.getContent());
		}
		return js;
	}

}
